package loger

import (
	"fmt"
	"os"
	"path"
)

//FileLog 文件日志
type FileLog struct {
	filePath string
	fileName string
	fileSize Loglevel
	fileObj  *os.File
	logLevel Loglevel
}

//NewFileLog 构造函数
func NewFileLog(filePath, fileName, logLevel string, fileSize Loglevel) *FileLog {
	level := findlevel(logLevel)
	path := path.Join(filePath, fileName)
	fileObj, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		fmt.Printf("Openfile failed,err:%v", err)
		panic("open file failed")
	}
	return &FileLog{
		filePath: filePath,
		fileName: fileName,
		fileSize: fileSize,
		fileObj:  fileObj,
		logLevel: level,
	}
}

//Debug 函数
func (l *FileLog) Debug(msg string) {
	fprint(l, DEBUG, msg)
}

//Trace 函数
func (l *FileLog) Trace(msg string) {
	fprint(l, TRACE, msg)
}

//Info 函数
func (l *FileLog) Info(msg string) {
	fprint(l, INFO, msg)
}

//Warning 函数
func (l *FileLog) Warning(msg string) {
	fprint(l, WARNING, msg)
}

//Error 函数
func (l *FileLog) Error(msg string) {
	fprint(l, ERROR, msg)
}

//Fatal 函数
func (l *FileLog) Fatal(msg string) {
	fprint(l, FATAL, msg)
}
